﻿//using IdentityModel.Client;
//using Microsoft.AspNetCore.Authentication;
//using Microsoft.AspNetCore.Authentication.Cookies;
//using Microsoft.AspNetCore.Mvc;
//using RuoVea.ExConfig;
//using RuoVea.ExUtil;
//using System.Security.Claims;

//namespace RuoVea.Gantt.Controllers
//{
//    //https://my.ruovea.com/auth/login?ReturnUrl=/connect/authorize/callback?client_id=MvcRoot.RuoVea.com.Gantt&redirect_uri=https://www.ruovea.com/gantt/signin-oidc&response_type=code&scope=openid%20profile%20offline_access%20orderApi
//    // https://localhost:7188/connect/authorize?client_id=MvcRoot.RuoVea.com.Gantt&redirect_uri=https://localhost:8896/signin-oidc&response_type=code&state=xyz&scope=orderApi
//    //https://my.ruovea.com/connect/authorize?client_id=MvcRoot.RuoVea.com.Gantt&redirect_uri=https://www.ruovea.com/gantt/signin-oidc&response_type=code&scope=openid profile offline_access orderApi
//    public class SigninOidcController : Controller
//    {
//        string authority = Appsettings.app("Authority:AuthorityUrl")?.ToString(); // 授权服务器地址 //"https://localhost:7188";
//        string clientId = Appsettings.app("Authority:ClientId")?.ToString();//"MvcClient";
//        string clientSecret = Appsettings.app("Authority:ClientSecret")?.ToString();// "codetest_secret";
//        string callbackPath = Appsettings.app("Authority:CallbackPath")?.ToString();// "/signin-oidc";
//        string responseType = Appsettings.app("Authority:ResponseType")?.ToString();// "code";
//        string hostUrl = Appsettings.app("BaseFile:HostUrl")?.ToString();

//        [Route("/signin-oidc")]
//        public async Task IndexAsync(string code, string scope, string state, string session_state)
//        {
//            if (HttpContext.User.Identity.IsAuthenticated)
//            {
//                var tt = HttpContext.Response;
//            }
//            else
//            {
//                var cookies = HttpContext.Request.Cookies;

//                Console.WriteLine("/signin-oidc");
//                var client = new HttpClient();
//                var disco = await client.GetDiscoveryDocumentAsync(authority);
//                Console.WriteLine("IsError =>" + (disco.IsError == false));
//                if (disco.IsError)
//                    throw new Exception(disco.Error);
 
//                //var token = await client.RequestAuthorizationCodeTokenAsync(new AuthorizationCodeTokenRequest()
//                //{
//                //    Address = disco.TokenEndpoint,

//                //    ClientId = clientId,
//                //    ClientSecret = clientSecret,//客户端密码
//                //    RedirectUri = $"{hostUrl}/signin-oidc",

//                //    Code = code,

//                //    //CodeVerifier=code,
//                //});

//                var token = await client.RequestAuthorizationCodeTokenAsync(new AuthorizationCodeTokenRequest()
//                {
//                    Address = disco.TokenEndpoint,
//                    ClientId = clientId,
//                    //客户端密码
//                    ClientSecret = clientSecret,
//                    GrantType = "client_credentials",
//                    Code = code,
//                    RedirectUri = $"https://localhost:8896/gantt/signin-oidc",
            

//                });

//                if (token.IsError)
//                {
//                    HttpContext.Response.WriteAsJsonAsync(token);
//                }
//                else
//                {
//                    Console.WriteLine("token =>" + token.Json2Str());
//                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
//                    ClaimsPrincipal claims = new ClaimsPrincipal(identity) { 
                    
//                    };
//                    await HttpContext.SignInAsync(identity.AuthenticationType,
//                        claims,
//                        new AuthenticationProperties
//                        {
//                            IsPersistent = false,//关闭浏览器 Cookies自动清除 
//                            RedirectUri = "/Home/Index",
//                            ExpiresUtc = new System.DateTimeOffset(dateTime: DateTime.Now.AddHours(6)),
//                        });
//                    //await HttpContext.SignInAsync(identity.AuthenticationType, new ClaimsPrincipal(identity), null);
//                    HttpContext.Response.Redirect(hostUrl);
//                }
//            }
//        }
//    }
//}
